home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2003 August / MW 8 2003 CD1.iso / Inside Macworld / Product News / gimp-1.2.4.sit / gimp-1.2.4 / libgimp / gimpsignal.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-11-09  |  3.5 KB  |  95 lines

  1. /* LIBGIMP - The GIMP Library                                                   
  2.  * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball                
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Lesser General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.             
  8.  *                                                                              
  9.  * This library is distributed in the hope that it will be useful,              
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of               
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU            
  12.  * Library General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Lesser General Public
  15.  * License along with this library; if not, write to the
  16.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.  * Boston, MA 02111-1307, USA.
  18.  *
  19.  * $Revision: 1.7.2.1 $
  20.  */                                                                             
  21.  
  22. #include <glib.h>
  23.  
  24. #ifndef G_OS_WIN32
  25.  
  26. #include "gimpsignal.h"
  27.  
  28.  
  29. /* Courtesy of Austin Donnelly 06-04-2000 to address bug #2742 */
  30.  
  31. /** 
  32.  * gimp_signal_private: 
  33.  * @signum: Selects signal to be handled see man 5 signal (or man 7 signal)
  34.  * @handler: Handler that maps to signum. Invoked by O/S. 
  35.  *           Handler gets signal that caused invocation. Corresponds
  36.  *           to the @sa_handler field of the @sigaction struct.
  37.  * @flags: Preferences. OR'ed SA_<xxx>. See man sigaction. Corresponds
  38.  *         to the @sa_flags field of the @sigaction struct.
  39.  *
  40.  * This function furnishes a workalike for signal(2) but
  41.  * which internally invokes sigaction(2) after certain
  42.  * sa_flags are set; these primarily to ensure restarting
  43.  * of interrupted system calls. See sigaction(2)  It is a 
  44.  * aid to transition and not new development: that effort 
  45.  * should employ sigaction directly. [gosgood 18.04.2000] 
  46.  *
  47.  * Cause @handler to be run when @signum is delivered.  We
  48.  * use sigaction(2) rather than signal(2) so that we can control the
  49.  * signal handler's environment completely via @flags: some signal(2)
  50.  * implementations differ in their sematics, so we need to nail down
  51.  * exactly what we want. [austin 06.04.2000]
  52.  *
  53.  * Returns: A reference to the signal handling function which was
  54.  *          active before the call to gimp_signal_private().
  55.  */
  56. GimpSignalHandlerFunc
  57. gimp_signal_private (gint                   signum,
  58.              GimpSignalHandlerFunc  handler,
  59.              gint                   flags)
  60. {
  61.   gint ret;
  62.   struct sigaction sa;
  63.   struct sigaction osa;
  64.  
  65.   /*  The sa_handler (mandated by POSIX.1) and sa_sigaction (a
  66.    *  common extension) are often implemented by the OS as members
  67.    *  of a union.  This means you CAN NOT set both, you set one or
  68.    *  the other.  Caveat programmer!
  69.    */
  70.  
  71.   /*  Passing gimp_signal_private a gimp_sighandler of NULL is not
  72.    *  an error, and generally results in the action for that signal
  73.    *  being set to SIG_DFL (default behavior).  Many OSes define
  74.    *  SIG_DFL as (void (*)()0, so setting sa_handler to NULL is
  75.    *  the same thing as passing SIG_DFL to it.
  76.    */
  77.   sa.sa_handler = handler;
  78.  
  79.   /*  Mask all signals while handler runs to avoid re-entrancy
  80.    *  problems.
  81.    */
  82.   sigfillset (&sa.sa_mask);
  83.  
  84.   sa.sa_flags = flags;
  85.  
  86.   ret = sigaction (signum, &sa, &osa);
  87.  
  88.   if (ret < 0)
  89.     g_error ("unable to set handler for signal %d\n", signum);
  90.  
  91.   return (GimpSignalHandlerFunc) osa.sa_handler;
  92. }
  93.  
  94. #endif /* !G_OS_WIN32 */
  95.